package com.rock.code.province.hn.coll;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;

import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author zhangli
 * @version V1.0
 * @create 2019-05-08 11:02
 **/
public class ArrayCollImpl {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        //认证信息对象，用于包含访问翻译服务的用户名和密码
        SSLConnectionSocketFactory scsf = null;
        try {

            scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(
                    null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);


        }catch (Exception e){
            e.printStackTrace();
        }
        String path = "https://10.230.28.60:8888/frameHeader.html";
//        String path = "http://10.230.39.121/html/login.html";
        //1.创建客户端访问服务器的httpclient对象   打开浏览器
//        HttpClient httpclient = new DefaultHttpClient();
        HttpClient httpclient = HttpClients.custom().setSSLSocketFactory(scsf).build();

        //2.以请求的连接地址创建get请求对象     浏览器中输入网址
        HttpGet httpget = new HttpGet(path);

        //username:password--->访问的用户名，密码,并使用base64进行加密，将加密的字节信息转化为string类型，encoding--->token
        String encoding = DatatypeConverter.printBase64Binary("y_chenyf:hndl2019".getBytes("UTF-8"));

        httpget.setHeader("Authorization", "Basic " +encoding);
        //3.向服务器端发送请求 并且获取响应对象  浏览器中输入网址点击回车
        SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
        HttpResponse response = httpclient.execute(httpget);
        Header[] hs  = response.getAllHeaders();
        System.out.println(response.getAllHeaders());
        //4.获取响应对象中的响应码
        StatusLine statusLine = response.getStatusLine();//获取请求对象中的响应行对象
        int responseCode = statusLine.getStatusCode();//从状态行中获取状态码

        System.out.println(responseCode);
        if (responseCode == 200) {
            //5.  可以接收和发送消息
            HttpEntity entity = response.getEntity();
            //6.从消息载体对象中获取操作的读取流对象
            InputStream input = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(input));
            while (true){
                String str1 = br.readLine();
                if(str1!=null){
                    String result = new String(str1.getBytes("gbk"), "utf-8");
                    System.out.println("" + result);
                }else {
                    break;
                }
            }

            br.close();
            input.close();
        } else {
            System.out.println("响应失败!");
        }
    }
}
